<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/29 0029
 * Time: 下午 1:31
 */
namespace Admin\Controller;
use Think\Controller;

class SiteController extends Controller
{

    public function getCountSite(){
        //  获取数据库新闻信息
        $newsT=D("site");
        $list=$newsT->select();
        $this->assign("list",$list);
        $this->display();
    }

    public function getSite()
    {
        //  获取数据库网站信息
        $admin = $_GET['id'];

        $site=D("site");

        $list=$site->where(['id'=>$admin])->select();

        if (!empty($list)){
            $this->assign("list",$list);
            //传输皮肤图片
            if ($list[0]['skin'] == 'Default'){
                $this->assign("skin","/images/default.png");
                $this->display();
            }
            else{
                $this->assign("skin","/images/blue.png");
                $this->display();
            }
        }
        else{
            $this->error('查看失败');
        }

    }

    public function addSite(){
        //创建网站
        $data['cid'] = $_SESSION['user'];
        $data['name'] = $_POST['name'];
        $data['skin'] = $_POST['skin'];
        $data['customs'] = $_POST['customs'];
        $data['title'] = $_POST['title'];
        $data['domain'] = $_POST['domain'];

        $site=D("site");
        if ($data['name'] != null && $data['skin']!=null && $data['customs']!=null && $data['domain']!=null){

            $re=$site->add($data);

            if(!empty($re)){
                $this->success('创建成功','getCountSite');
            }
            else{
                $this->error('创建失败','getCountSite');
            }
        }
        else{
            $this->error('请填完选项！','/site_add.html');
        }


    }
    public function updateSite(){
        //传输网站参数，申请网站更改
        $admin = $_GET['id'];

        $site=D("site");

        $list=$site->where(['cid'=>$admin])->select();

        if (!empty($list)){
            $this->assign("list",$list);
            //传输皮肤图片
            if ($list[0]['skin'] == 'Default'){
                $this->assign("skin",["name"=>"Default","img"=>"/images/default.png"]);
                $this->assign("skin2",["name"=>"Blue","img"=>"/images/blue.png"]);
                $this->display();
            }
            else{
                $this->assign("skin",["name"=>"Blue","img"=>"/images/blue.png"]);
                $this->assign("skin2",["name"=>"Default","img"=>"/images/default.png"]);
                $this->display();
            }

        }
        else{
            $this->error('获取数据库信息失败');
        }
    }

    public function upSite(){
        //修改网站参数
        $id = $_POST['id'];
        $data['name'] = $_POST['name'];
        $data['skin'] = $_POST['skin'];
        $data['customs'] = $_POST['customs'];
        $data['title'] = $_POST['title'];
        $data['domain'] = $_POST['domain'];

        $site = D("site");

        $re=$site->where("id='$id'")->save($data);

        if(!empty($re)){
            $this->success('更改成功','getCountSite');
        }
        else{
            $this->error('更改失败','getCountSite');
        }

    }

    public function delete(){
        //  删除网站操作
        $id=$_GET["id"];

        $site=D("site");

        $de = $site->delete($id);

        if(!empty($de)){
            $this->success('删除成功','getCountSite');
        }
        else{
            $this->error('删除失败','getCountSite');
        }

    }
}